package com.situ.project1.controller;

import com.github.pagehelper.PageInfo;
import com.situ.project1.pojo.Order;
import com.situ.project1.service.OrderService;
import com.situ.project1.util.PaginateInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static java.lang.Integer.parseInt;

/**
 * @author:yzy
 * @date:2024/8/10
 */
@RestController
@RequestMapping("/api/order")
public class OrderController {
    private OrderService orderService;

    @Autowired

    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    @GetMapping
    public Map<String, Object> findAll(
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "2147483647") Integer pageSize,
            Order order
    ) {

        PaginateInfo pi = new PaginateInfo(pageNo, pageSize);
        List<Order> orders = orderService.findAll(pi, order);
        PageInfo<Order> pif = new PageInfo<>(orders);
        Map<String, Object> map = Map.of("total", pif.getTotal(), "pageNo", pif.getPageNum(), "pageSize", pif.getPageSize());
        if (orders.isEmpty()){
            return Map.of("success", false, "rows", orders, "pi", map);
        }else {
            return Map.of("success", true, "rows", orders, "pi", map);
        }
    }

    @PostMapping
    public Map<String, Object> add(@RequestBody Order order) {
        if (Objects.isNull(order.getOrderId()) || order.getOrderId().isEmpty() ||
                Objects.isNull(order.getOrderName()) || order.getOrderName().isEmpty() ||
                Objects.isNull(order.getOrderModel()) || order.getOrderModel().isEmpty() ||
                Objects.isNull(order.getOrderStatus()) || order.getOrderStatus().isEmpty() ||
                Objects.isNull(order.getOrderTime()) ||
                Objects.isNull(order.getEmpId()) || order.getEmpId().isEmpty()) {
            return Map.of("success", false, "msg", "参数不能为空");
        } else {

            boolean success = orderService.add(order);
            return Map.of("success", success);
        }
    }

    @PutMapping
    public Map<String, Object> update(@RequestBody Order order) {
        if (order.getOrderId() == null
                || order.getOrderName() == null
                || order.getOrderModel() == null
                || order.getOrderStatus() == null
                || order.getOrderTime() == null
                || order.getEmpId() == null) {
            return Map.of("success", false, "msg", "参数不能为空");
        } else {
            boolean success = orderService.update(order);
            return Map.of("success", success);
        }


    }

    @DeleteMapping
    public Map<String, Object> delete(@RequestBody Integer ids[]) {
        boolean success = orderService.delete(ids);
        return Map.of("success", success);
    }
    @DeleteMapping("/deleteById")
    public Map<String, Object> deleteById(@RequestParam Integer id) {
        boolean success = orderService.deleteById(id);
        return Map.of("success", success);
    }
    //    寻找最早的订单根据empId
    @GetMapping("/firstOrder")
    public Map<String, Object> findEarliest(@RequestParam String orderId) {
        Order order = orderService.findEarliest(orderId);
        if (order == null) {
            // 如果order为null，返回一个表示失败的map，并附带错误信息
            return Map.of("success", false, "message", "未找到订单");
        } else {
            // 如果order不为null，则正常返回
            return Map.of("success", true, "rows", order);
        }
    }
//    完成订单，并且给汽车表的预约数量加一
    @PostMapping("/finish")
    public Map<String, Object> finish(@RequestParam String id,@RequestParam String model){
        boolean success = orderService.finish(id,model);
        return Map.of("success", success);
    }
}
